export function parseAsJson(content: string): any {
  try {
    // 提取 JSON 字符串部分
    const lines = content.split('\n')
    if (
      lines.length < 2 ||
      !lines[0].trim().startsWith('```custom-') ||
      !lines[lines.length - 1].trim().startsWith('```')
    ) {
      throw new Error('Invalid content format')
    }

    const jsonContent = lines.slice(1, -1).join('\n').trim()

    const parsedJson = JSON.parse(jsonContent)
    return parsedJson
  } catch (error) {
    console.error(`Error parsing JSON: ${error}\nContent: ${content}`)
    throw error
  }
}

export function parseOptions(blockContent: string): any {
  try {
    const firstLine = blockContent.split('\n')[0].trim()

    // Check if the line starts with ```
    if (!firstLine.startsWith('```')) {
      throw new Error('Invalid block content: must start with ```')
    }

    // Remove the leading ``` and trailing ```
    const content = firstLine.slice(3).trim()

    // Split the content into parts
    const [name] = content.split(':')
    const remain = content.slice(name.length + 1)
    const [functionName] = remain.split('(')

    const paramsStr = remain.slice(functionName.length).slice(1, -1).trim()

    //console.log("paramStr-", paramsStr);
    // Parse the parameters as JSON
    const params = JSON.parse(paramsStr)

    // Return the parsed object
    return { name, functionName, params }
  } catch (error) {
    console.error('Error parsing block content:', error, blockContent)
    throw new Error('Failed to parse block content')
  }
}
